const code = require("../models/code")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

router.post('/', (req, res) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            code.create({
                name: fields.name,
                js_content: fields.js,
                html_content: fields.html,
                css_content: fields.css
            }, (err, doc) => {
              if(err) {
                res.json({'state': false, 'msg': err})
              }else {
                  res.json({
                      'state': true,
                      'msg': doc
                  })
              }
            })
        }

    });
});

module.exports = router;